<?php
class ctrl_news extends ctrl_base
{
    private static $dirName = 'news';
    /**
     * 图片上传
     */
    public static function imageUplody(){
    	if($_FILES){
    		$config = array(
    				"savePath" => UPLOAD_FILES_PATH,
    				"maxSize" => 1024,
    				"allowFiles" => array(".png", ".jpg", ".jpeg")
    		);
    		$up = new framework_static_uploader("Filedata", $config);
    		$info = $up->getFileInfo();
    		if($info['state'] != 'SUCCESS'){
    			framework_static_function::output_jsonp($info);
    		}
    		$info['url'] = trim($info['url'],'.');
    		framework_static_function::output_jsonp($info);
    	}
    }
   /**
    * 获取被动回复微信用户的图文消息列表
    * @param string $openid
    * @param int $catId
    * @return string|Ambigous <sring, mixed, string>
    */
   public static function getNewsList($openid,$catId,$pageSize=10) {
   		$content = '';
   		if(empty($openid) || intval($catId) == 0) {  //openid和category_id不为空
   			return $content;
   		}
   		$pageSize = $pageSize>10? 10:$pageSize;//微信最多支持10条消息
   		if(!empty($_SESSION['wx_userinfo']['user_type'])) {
   			$visible = self::get_visible_str($openid, $_SESSION['wx_userinfo']['user_type']);
   		} else {
   			$userInfo = model_user::get_user_by_openid($openid,WEIXINHAO);
   			if(!empty($userInfo['user_type'])) {
   				$visible = self::get_visible_str($openid, $userInfo['user_type']);
   			} else {
   				$visible = "*_*";//微信可见用户，*_* 表示所有用户可见
   			}
   		}
   		$news_list = model_news::get_news_by_catId($catId,$pageSize,true,$visible);
   		$count = count($news_list);
   		if($count > 0) {
   			self::smarty()->assign('toUser',$openid);
   			self::smarty()->assign('fromUser',WEIXINHAO);
   			self::smarty()->assign('time',time());
   			self::smarty()->assign('articleCount',$count);
   			self::smarty()->assign('news_list',$news_list);
   			self::smarty()->assign('hostUrl',HOST_URL);
   			$content = self::smarty()->fetch('news.xml');
   			return $content;
   		} else {  //栏目的新闻列表为空
   			return $content;
   		}
   }
	/**
	 * @author Jackie Yang
	 * 获取权限字符串
	 * @param string $user_type 1:内部用户,2：外部用户
	 */
	private static function get_visible_str($openid,$user_type) {
		$visible = "*_*";//微信可见用户，*_* 表示所有用户可见
		$table = $user_type == 1 ? 'weixin_sales_user' : 'weixin_register_user';
		$userInfo = model_user::get_register_userinfo_by_openid ( $openid, $table );
		//framework_static_function::write_log(__METHOD__."-userinfo:".json_encode($userInfo),"sql");
		if (!empty($userInfo ['user_category_id'])) {
			$visible = $user_type . "_" . $userInfo ['user_category_id'];
		}
		//framework_static_function::write_log(__METHOD__."-visible:".$visible,"sql");
		return $visible;
	}
   
   /**
    * 查看新闻详情
    */
   public static function info() {
   		$id = isset($_GET['id'])? intval($_GET['id']):0;
   		if($id == 0) {
   			header("Content-type: text/html; charset=utf-8");
   			exit("非法访问！");
   		}
   		$newsInfo = model_news::getNewsInfoById($id);
   		$newsInfo['content'] = htmlspecialchars_decode($newsInfo['content']);
   		$meta = array('title'=>$newsInfo['title'],'keywords'=>'','description'=>'');
   		self::smarty()->assign('meta',$meta);
   		self::smarty()->assign('newsInfo',$newsInfo);
   		self::smarty()->display("newsinfo.html");
   }
   /**
    * 资质荣誉
    */
   public static function honors() {
       if(empty($_SESSION['wx_userinfo']['openid'])) {
           setcookie('backurl',HOST_URL.'/?'.$_SERVER['QUERY_STRING'],0);
           ctrl_weixin::oauth();
       }else{
           setcookie('backurl','');
       }
   		$catId = model_news::get_catid_by_catName('资质荣誉');
   		$pageSize = $pageSize>10? 10:$pageSize;//微信最多支持10条消息
       $openid = $_SESSION['wx_userinfo']['openid'];
       if(!empty($_SESSION['wx_userinfo']['user_type'])) {
           $visible = self::get_visible_str($openid, $_SESSION['wx_userinfo']['user_type']);
       } else {
           $userInfo = model_user::get_user_by_openid($openid,WEIXINHAO);
           if(!empty($userInfo['user_type'])) {
               $visible = self::get_visible_str($openid, $userInfo['user_type']);
           } else {
               $visible = "*_*";//微信可见用户，*_* 表示所有用户可见
           }
       }
       framework_static_function::write_log(__METHOD__."-visible:".$visible,"sql");
   		$news_list = model_news::get_news_by_catId($catId,$pageSize,false,$visible);
		self::smarty()->assign('catName','资质荣誉');
		self::smarty()->assign('news_list',$news_list);
		self::smarty()->assign('hostUrl',HOST_URL);	
   		self::smarty()->display("newslist.html");
   }
    /**
    * 应用案例
    */
   public static function cases() {
       if(empty($_SESSION['wx_userinfo']['openid'])) {
           setcookie('backurl',HOST_URL.'/?'.$_SERVER['QUERY_STRING'],0);
           ctrl_weixin::oauth();
       }else{
           setcookie('backurl','');
       }
   		$catId = model_news::get_catid_by_catName('应用案例');
   		$pageSize = $pageSize>10? 10:$pageSize;//微信最多支持10条消息
        $openid = $_SESSION['wx_userinfo']['openid'];

       if(!empty($_SESSION['wx_userinfo']['user_type'])) {
           $visible = self::get_visible_str($openid, $_SESSION['wx_userinfo']['user_type']);
       } else {
           $userInfo = model_user::get_user_by_openid($openid,WEIXINHAO);
           if(!empty($userInfo['user_type'])) {
               $visible = self::get_visible_str($openid, $userInfo['user_type']);
           } else {
               $visible = "*_*";//微信可见用户，*_* 表示所有用户可见
           }
       }
       framework_static_function::write_log(__METHOD__."-visible:".$visible,"sql");
   		$news_list = model_news::get_news_by_catId($catId,$pageSize,false,$visible);
		self::smarty()->assign('catName','应用案例');
		self::smarty()->assign('news_list',$news_list);
		self::smarty()->assign('hostUrl',HOST_URL);	
   		self::smarty()->display("newslist.html");
   }
 	/**
    * 价格查询-业务查询
    */
   public static function prices() {
       if(empty($_SESSION['wx_userinfo']['openid'])) {
           setcookie('backurl',HOST_URL.'/?'.$_SERVER['QUERY_STRING'],0);
           ctrl_weixin::oauth();
       }else{
           setcookie('backurl','');
       }
	   if(empty($_SESSION['wx_userinfo']['openid'])) {
    		setcookie('backurl',HOST_URL.'/?'.$_SERVER['QUERY_STRING'],0);
    		ctrl_weixin::oauth();
    	}else{
    		setcookie('backurl','');
    		unset($_SESSION['backurl']);
    	}
    	$open_id = $_SESSION['wx_userinfo']['openid'];
   		$catId = model_news::get_catid_by_catName('业务查询');
   		$pageSize = $pageSize>10? 10:$pageSize;//微信最多支持10条消息
   		$news_list = model_news::get_news_by_catId_openId($open_id,$catId,$pageSize,false);
		self::smarty()->assign('catName','业务查询');
		self::smarty()->assign('news_list',$news_list);
		self::smarty()->assign('hostUrl',HOST_URL);	
   		self::smarty()->display("newslist.html");
   }
    /**
    * 获取“我要交流”话题类型
    */
   public static function get_topic_list($openid,$catId,$pageSize=10) {
   		$content = '';
   		if(empty($openid) || intval($catId) == 0) {  //openid和category_id不为空
   			return $content;
   		}
   		$pageSize = $pageSize>10? 10:$pageSize;//微信最多支持10条消息
   		$news_list = model_news::get_news_by_catId($catId,$pageSize);
   		$count = count($news_list);
   		if($count > 0) {
   			self::smarty()->assign('toUser',$openid);
   			self::smarty()->assign('fromUser',WEIXINHAO);
   			self::smarty()->assign('time',time());
   			self::smarty()->assign('articleCount',$count);
   			self::smarty()->assign('news_list',$news_list);
   			self::smarty()->assign('hostUrl',HOST_URL);
   			self::smarty()->assign('goto_chat',1);
   			$content = self::smarty()->fetch('news.xml');
   			return $content;
   		} else {  //栏目的新闻列表为空
   			return $content;
   		}
   }
    /**
     * 资质荣誉
     */
    public static function information() {
        if(empty($_SESSION['wx_userinfo']['openid'])) {
            setcookie('backurl',HOST_URL.'/?'.$_SERVER['QUERY_STRING'],0);
            ctrl_weixin::oauth();
        }else{
            setcookie('backurl','');
        }
        $catId = model_news::get_catid_by_catName('行情资讯');
	    //获取子类
        $sonList = model_news::getSonList($catId);
	    self::smarty()->assign('catName','行情资讯');
        self::smarty()->assign('sonList',$sonList);
        self::smarty()->assign('hostUrl',HOST_URL);
	    self::smarty()->display("sonlist.html");
    }

    public static function informationson() {
        $catId = isset($_GET['id'])? intval($_GET['id']):0;
        if($catId == 0) {
            header("Content-type: text/html; charset=utf-8");
            exit("非法访问！");
        }
        $catName = model_news::get_catName_by_catid($catId);
        $pageSize = $pageSize>10? 10:$pageSize;//微信最多支持10条消息
        $openid = $_SESSION['wx_userinfo']['openid'];
        framework_static_function::write_log(__METHOD__."-user_type:".json_encode($_SESSION['wx_userinfo']),"sql");
        if(!empty($_SESSION['wx_userinfo']['user_type'])) {
            $visible = self::get_visible_str($openid, $_SESSION['wx_userinfo']['user_type']);
        } else {
            $userInfo = model_user::get_user_by_openid($openid,WEIXINHAO);
            if(!empty($userInfo['user_type'])) {
                $visible = self::get_visible_str($openid, $userInfo['user_type']);
            } else {
                $visible = "*_*";//微信可见用户，*_* 表示所有用户可见
            }
        }
        framework_static_function::write_log(__METHOD__."-visible:".$visible,"sql");

        $news_list = model_news::get_news_by_catId($catId,$pageSize,false,$visible);
        self::smarty()->assign('catName',$catName);
        self::smarty()->assign('news_list',$news_list);
        self::smarty()->assign('hostUrl',HOST_URL);
        self::smarty()->display("newslist.html");
    }




}
